package com.youxiake.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * lock 锁具备可重入性 特征：锁是可以传递的（方法递归传递）
 * @author jxbai
 * @date 2021/12/14
 */
public class ReentrantLockDemo implements Runnable {

    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        set();
    }

    public void set() {
        lock.lock();
        try {
            System.out.println("set方法");
            get();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get() {
        lock.lock();
        try {
            System.out.println("lock 可以具备可冲入性 -- get方法");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        new Thread(new ReentrantLockDemo()).start();
    }

}
